/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.homesoft.component;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.Serializable;
import java.util.Collections;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author phucan
 */
public class HmSuggestedCombo extends JPanel implements Serializable{

    JTextField txtInput = new JTextField();
    JComboBox cmbHint = new JComboBox();
    private boolean hide_flag = false;
    Vector<String> v;

    public HmSuggestedCombo() {
        super();
        // this.setLayout(new Ab);
        this.setLayout(new BorderLayout());
  

        this.add(cmbHint, BorderLayout.NORTH);
       // this.add(txtInput, BorderLayout.NORTH);
        // this.setSize(220, 45);
       // this.add(cmbHint);
       // this.add(txtInput);
        cmbHint.setEditable(true);
        txtInput = (JTextField) cmbHint.getEditor().getEditorComponent();
        txtInput.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                EventQueue.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        String text = txtInput.getText();
                        if (text.length() == 0) {
                            cmbHint.hidePopup();
                            setModel(new DefaultComboBoxModel(v), "");
                        } else {
                            DefaultComboBoxModel m = getSuggestedModel(v, text);
                            if (m.getSize() == 0 || hide_flag) {
                                cmbHint.hidePopup();
                                hide_flag = false;
                            } else {
                                setModel(m, text);
                                cmbHint.showPopup();
                            }
                        }
                    }
                });
            }

            @Override
            public void keyPressed(KeyEvent e) {
                String text = txtInput.getText();
                int code = e.getKeyCode();
                if (code == KeyEvent.VK_ENTER) {
                    if (!v.contains(text)) {
                        v.addElement(text);
                        Collections.sort(v);
                        setModel(getSuggestedModel(v, text), text);
                    }
                    hide_flag = true;
                } else if (code == KeyEvent.VK_ESCAPE) {
                    hide_flag = true;
                } else if (code == KeyEvent.VK_RIGHT) {
                    for (int i = 0; i < v.size(); i++) {
                        String str = v.elementAt(i);
                        if (str.contains(text)) {
                            cmbHint.setSelectedIndex(-1);
                            txtInput.setText(str);
                            return;
                        }
                    }
                }
            }
        });

        this.addPropertyChangeListener(new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
               change(evt);
            }
        });
    }

    private static DefaultComboBoxModel getSuggestedModel(java.util.List<String> list, String text) {
        DefaultComboBoxModel m = new DefaultComboBoxModel();
        text=text.toLowerCase();
        for (String s : list) {
            if (s.toLowerCase().contains(text)) {
                m.addElement(s);
            }
        }
        return m;
    }

    private void setModel(DefaultComboBoxModel mdl, String str) {
        cmbHint.setModel(mdl);
        cmbHint.setSelectedIndex(-1);
        txtInput.setText(str);
    }

    private void change(PropertyChangeEvent e) {
        txtInput.setBounds(0, 0, this.getSize().width - 15, this.getSize().height - 10);
        cmbHint.setBounds(0, 0, this.getSize().width - 15, this.getSize().height - 10);
    }

    public void setDataList(Vector v) {
        this.v = v;
        setModel(new DefaultComboBoxModel(v), "");
    }
    public String getText(){
        return txtInput.getText();
    }
    public void setText(String text) {
        txtInput.setText(text);
    }
    public int getSelectedIndex(){
        return cmbHint.getSelectedIndex();
    }
    public Object getSelectedItem(){
        return cmbHint.getSelectedItem();
    }
    
}
